//
//  LanguagePickerView.swift
//  koolcar
//
//  Created by xiantian on 2023/10/9.
//

import UIKit
 
typealias LanguagePickerViewBlock = (String) -> Void

class LanguagePickerView: UIViewController {
    var callBack: LanguagePickerViewBlock?
    var bkgBtn:UIButton!
    var contentView: UIView!
    var defaultCode: NSInteger = 86
    var contentH: CGFloat = 232.0
    
    var pickerView: UIPickerView!
    var dataArray: [String] = [
        MMLocalizedString("launch_hk", comment: "繁體中文"),
        MMLocalizedString("launch_cn", comment: "简体中文"),
        MMLocalizedString("launch_en", comment: "English")
    ]
    var bottomBtn: UIButton!
    var selectIndex: NSInteger = 999
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.clear
        let curLan = XTManageCenter.getAppleLanguages()
        switch curLan {
            case AppLanguage.english.rawValue:
                self.selectIndex = 2
            case AppLanguage.chinese_simple.rawValue:
                self.selectIndex = 1
            default:
                self.selectIndex = 0
        }
        
        //行高44， 底部按钮44 头部56
        let minC = max(self.dataArray.count, 3) //最小三行高度
        self.contentH = CGFloat(minC * 50) + 50.0 + 56.0 + IphoneXMarginBottom
     
        //背景按钮
        bkgBtn = UIButton.init(type: UIButtonType.custom)
        bkgBtn.frame = CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: self.view.frame.size.height)
        bkgBtn.backgroundColor =  UIColor.init(red: 0, green: 0, blue: 0, alpha: 0)
        self.view .addSubview(bkgBtn)
        bkgBtn.addTarget(self, action: #selector(hiddenViewAction), for: UIControlEvents.touchUpInside)
        
        //内容视图
        contentView = UIView.init(frame: CGRect.init(x: 0, y: kScreenHeight, width: kScreenWidth , height: contentH))
        contentView.backgroundColor = UIColor.white
        bkgBtn.addSubview(contentView)
        
        //heaer
        let headerBtn = UIButton()
        headerBtn.frame = CGRect.init(x: 0, y: 0, width: kScreenWidth, height: 56)
        headerBtn.setup_titleLabel(text: MMLocalizedString("setting_select_lan", comment: "选择语言"))
        headerBtn.setup_titleColor(color: Theme.titleLabColor)
        headerBtn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCMedium, size: 16)
        headerBtn.backgroundColor = UIColor.white
        contentView.addSubview(headerBtn)

        pickerView = UIPickerView.init(frame: CGRect.init(x: 0, y: 0, width: kScreenWidth, height: 200))
        pickerView.delegate = self
        pickerView.dataSource = self
        contentView.addSubview(pickerView)
        
        if self.dataArray.count > self.selectIndex {
            pickerView.selectRow(self.selectIndex, inComponent: 0, animated: true)
        }
        
        bottomBtn = UIButton()
        bottomBtn.setup_titleLabel(text: MMLocalizedString("confirm", comment: "确定"))
        bottomBtn.setup_titleColor(color: Theme.mainColor)
        bottomBtn.titleLabel?.font = UIFont.init(name: Theme.PingFangSCMedium, size: 14)
        bottomBtn.addTarget(self, action: #selector(bottomBtnAction), for: .touchUpInside)
        contentView.addSubview(bottomBtn)
        
        self.addSnpLayout()
    }
 
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        self.startAnimate()
    }
    
    
    private func addSnpLayout(){
        bkgBtn.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        contentView.snp.makeConstraints { make in
            make.left.right.equalTo(0)
            make.bottom.equalTo(contentH)
            make.height.equalTo(contentH)
        }
        
        pickerView.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.width.equalTo(kScreenWidth)
            make.top.equalTo(56)
        }
        
        bottomBtn.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.top.equalTo(self.pickerView.snp.bottom)
            make.height.equalTo(44)
            make.bottom.equalTo(-IphoneXMarginBottom-4)
        }
        self.view.layoutIfNeeded()
        contentView.layer.cornerRadius = 10
        contentView.layer.masksToBounds = true
        bottomBtn.topBorder(width: 0.5, borderColor: Theme.lineColor)
    }
    
    ///
    @objc func bottomBtnAction(){
        switch self.selectIndex {
            case 0:
                //繁体中文
                XTManageCenter.setLocalDataString(aKey: UtilCenter.kAppLanguage, aValue: AppLanguage.chinese_hk.rawValue)
            case 1:
                //简体
                XTManageCenter.setLocalDataString(aKey: UtilCenter.kAppLanguage, aValue: AppLanguage.chinese_simple.rawValue)
            case 2:
                //english
                XTManageCenter.setLocalDataString(aKey: UtilCenter.kAppLanguage, aValue: AppLanguage.english.rawValue)
            default:
                ()
        }
        let lan = self.dataArray[self.selectIndex]
        self.callBack?(lan)
        self.hiddenViewAction()
    }
    
    
    private func startAnimate(){
        UIView.animate(withDuration: 0.24, delay: 0) {
            self.bkgBtn.backgroundColor = UIColor.init(hex: 0x000000, alpha: 0.6)
            self.contentView.snp.updateConstraints { make in
                make.bottom.equalTo(0)
            }
            self.view.layoutIfNeeded()
        }
    }
    
    @objc func hiddenViewAction(){
        UIView.animate(withDuration: 0.24, delay: 0) {
            self.bkgBtn.backgroundColor = UIColor.init(hex: 0x000000, alpha: 0)
            self.contentView.snp.updateConstraints { make in
                make.bottom.equalTo(self.contentH)
            }
            self.view.layoutIfNeeded()
        } completion: { flag in
            self.dismiss(animated: false, completion: nil)
        }
    }
}
 

extension LanguagePickerView: UIPickerViewDelegate, UIPickerViewDataSource {
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }
    
    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return self.dataArray.count
    }
    
    func pickerView(_ pickerView: UIPickerView, rowHeightForComponent component: Int) -> CGFloat {
        return 50
    }

    func pickerView(_ pickerView: UIPickerView, viewForRow row: Int, forComponent component: Int, reusing view: UIView?) -> UIView {
        let tempView = UILabel.init(frame: CGRect.init(x: 0, y: 0, width: kScreenWidth, height: 50))
        let model = self.dataArray[row]
        tempView.text = model
        tempView.backgroundColor = UIColor.clear
        tempView.font = UIFont.init(name: Theme.PingFangSCMedium, size: 20)
        tempView.textColor = Theme.titleLabColor
        tempView.textAlignment = .center
        return tempView
    }
    
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        self.selectIndex = row
    }
 
}
